package com.ruoyi.odorservice.controller;

import com.ruoyi.common.core.domain.Result;
import com.ruoyi.odorservice.entity.CraftOdor;
import com.ruoyi.odorservice.model.bean.CraftIndustryAssociationBean;
import com.ruoyi.odorservice.model.bean.CraftInfoListBean;
import com.ruoyi.odorservice.model.bean.CraftOdorListBean;
import com.ruoyi.odorservice.model.vo.CraftListVo;
import com.ruoyi.odorservice.model.vo.CraftOdorListVo;
import com.ruoyi.odorservice.service.CraftService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/craft")
@Api(tags = "工艺信息")
@AllArgsConstructor
@Validated
public class CraftController {

    private final CraftService craftService;

    @GetMapping("/list")
    @ApiOperation(value = "获取工艺列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page" , value = "页码", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "limit" , value = "条数", paramType = "query", dataType = "Integer", required = false),
            @ApiImplicitParam(name = "craftName" , value = "工艺名称", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "industryId" , value = "行业id", paramType = "query", dataType = "Integer", required = false)
    })
    public Result<CraftListVo> CraftList(Integer page, Integer limit, String craftName, Integer industryId){
        if(page == null) page = 1;
        if(limit == null) limit = 10000000;
        CraftInfoListBean craftInfoListBean = new CraftInfoListBean(page, limit, craftName, industryId);
        return craftService.craftList(craftInfoListBean);
    }

    @PostMapping("/addCraft")
    @ApiOperation(value = "添加工艺", httpMethod = "POST")
    public Result<Boolean> addCraft(@RequestBody CraftIndustryAssociationBean craftIndustryAssociationBean){
        if(craftIndustryAssociationBean.getCraftName().isEmpty()) return Result.fail("必填项为空");
        if(craftIndustryAssociationBean.getIndustryId() == null) return Result.fail("必填项为空");
        return craftService.addCraft(craftIndustryAssociationBean);
    }

    @PostMapping("/updateCraft")
    @ApiOperation(value = "修改工艺", httpMethod = "POST")
    public Result<Boolean> updateCraft(@RequestBody CraftIndustryAssociationBean craftIndustryAssociationBean){
        if(craftIndustryAssociationBean.getId() == null) return Result.fail("必填项为空");
        if(craftIndustryAssociationBean.getCraftName().isEmpty()) return Result.fail("必填项为空");
        return craftService.updateCraft(craftIndustryAssociationBean);
    }

    @PostMapping("/addOdor")
    @ApiOperation(value = "嗅辨员填写工艺下气味", httpMethod = "POST")
    public Result<Boolean> addOdor(@Valid @RequestBody CraftOdor craftOdor) {
        if(craftOdor.getOdorWeight() < 1 || craftOdor.getOdorWeight() > 5) return Result.fail("气味强度超出范围");
        return craftService.addOdor(craftOdor);
    }

    @GetMapping("/craftOdorList")
    @ApiOperation(value = "嗅辨员填写气味列表", httpMethod = "GET")
    public Result<CraftOdorListVo> craftOdorList(CraftOdorListBean craftOdorListBean) {
        if(craftOdorListBean == null) craftOdorListBean = new CraftOdorListBean();
        if(craftOdorListBean.getPage() == null) craftOdorListBean.setPage(1);
        if(craftOdorListBean.getLimit() == null) craftOdorListBean.setLimit(10000000);
        return craftService.craftOdorList(craftOdorListBean);
    }

}
